﻿CloudDisk.Util = (function(){
    var mask = null;
	
	return {
	    extend: function(c, p) {
			for(var key in p.prototype) {
				if(!( key in c.prototype)) {
					c.prototype[key] = p.prototype[key];
				}
			}
		},
		
	    showMask: function(){
		    if(!mask){
			    mask = $('<div class="page-mask"></div>').appendTo(document.body);
			}
			mask.show();
		},
		
		hideMask: function(){
		    mask.hide();
		},
		
		alert: function(msg){
		    new CloudDisk.MessageDialog(msg, '出错啦');
		},
		
		formatFileSize: function(size){
		    if(size < 1024 * 1024){
			    return Math.round(size / 1024 * 100) / 100 + 'KB';
			} else {
			    return Math.round(size / (1024 * 1024) * 100) / 100 + 'MB';
			}
		},
		
		getIconByExtension: function(ext){
		    var icons = CloudDisk.Setting.Icons;
			var path = CloudDisk.Setting.ICONS_PATH;
			var icon = icons.all; //default
		    if(ext == 'pdf'){
			    icon = icons.pdf;
			} else if(ext == 'txt'){
			    icon = icons.txt;
			} else if(ext == 'mp3'){
			    icon = icons.mp3;
			} else if(ext == 'xls'){
			    icon = icons.xls;
			}
			
			return {
			    small: path + icon.small,
				large: path + icon.large
			}
		},
		
		getIconByMimeType: function(type){
		    var icons = CloudDisk.Setting.Icons;
			var path = CloudDisk.Setting.ICONS_PATH;
			var icon = icons.all; //default
		    if(type == 'application/pdf'){
			    icon = icons.pdf;
			} else if(type.indexOf('text') == 0){
			    icon = icons.txt;
			} else if(type == 'audio/mpeg'){
			    icon = icons.mp3;
			} else if(type == 'application/vnd.ms-excel'){
			    icon = icons.xls;
			}
			
			return {
			    small: path + icon.small,
				large: path + icon.large
			}
		},
		
		getUrlParameter: function(name){
		    var queryString = window.location.toString().split("#");
			if(queryString.length > 1){
				var params = queryString[1].split("&");
				for(var i = 0; i< params.length; ++i){
					var param = params[i].split("=");
					if(param[0] == name){
						return unescape(param[1]);
					}
				}
			}
		},
		
		//only log in debug mode
		log: CloudDisk.Setting.DEBUG_MODE? (function(msg){
		    console.log(msg);
		}) : (function(){})
	};
})();